package com.miracle.erp.filter;

import java.io.IOException;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;


import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import com.miracle.erp.util.HttpFilter;

public class EncodingFilter extends HttpFilter  {

		private String encoding;

		@Override
		public void init() throws ServletException{
			// 加载字符编码
			encoding = getFilterConfig().getInitParameter("encoding");
			// 如果没有设置字符编码,默认设置utf-8
			if (encoding == null  || encoding.trim().equals("")) {
				encoding = "utf-8";
			}
		}

		@Override
		public void doFilter(ServletRequest request, ServletResponse response,
				FilterChain chain) throws IOException, ServletException {
			// 设置
			request.setCharacterEncoding(encoding);
			response.setCharacterEncoding(encoding);
			// 放行
			chain.doFilter(request, response);
		}
}
